package com.interview.score.controller;


import com.interview.score.common.MarkState;
import com.interview.score.common.Result;
import com.interview.score.common.ResultMessage;
import com.interview.score.common.StatusCode;
import com.interview.score.service.ExamRoomService;
import com.interview.score.utils.RedisKeyUtils;
import com.interview.score.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;

@RestController
@RequestMapping("/markProcess")
public class MarkProcessController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Environment environment;
    @Autowired
    private ExamRoomService examRoomService;
    @Autowired
    private TimeUtils timeUtils;
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @PostMapping("/getCurrentInterviewingExameeDrawLot")
    public String getCurrentInterviewingExameeDrawLot() {
        //组装ID
        String examRoomId =(String) redisTemplate.opsForValue().get(environment.getProperty("ExamRoomIdKey"));
        List range = redisTemplate.opsForList().range(environment.getProperty("ExameeSequenceKey")+"-"+examRoomId, 0, 1);
        return  range.get(0).toString();
    }

    @PostMapping("/getExaminerMarkProcess")
    public Result getExaminerMarkProcess(String exameeDrawLot) {
        String examRoomId =(String) redisTemplate.opsForValue().get(environment.getProperty("ExamRoomIdKey"));
        String exameeScoreKey = environment.getProperty("ExameeScoreKey");
        String key = exameeScoreKey+"-"+examRoomId+"-"+exameeDrawLot;
        Set keys = redisTemplate.opsForHash().keys(key);
        //获取总考官人数
        String totalExaminerAmount = environment.getProperty("TotalExaminerAmountKey") + "-" + redisTemplate.opsForValue().get(environment.getProperty("ExamRoomIdKey"));

        String amount = redisTemplate.opsForValue().get(totalExaminerAmount).toString();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 1; i <=Integer.parseInt(amount) ; i++) {
            if (keys.contains(i+"")) {
                map.put(i, MarkState.Marked);
            }else {
                map.put(i, MarkState.UnMarked);
            }
        }
        return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,map);
    }

    @PostMapping("/mark")
    public Result mark(String examinerDrawLot,String exameeDrawLot,String score){
        //创建考官打分hash
        redisTemplate.opsForHash().put((String)environment.getProperty("ExaminerMarkKey")+"-"+redisTemplate.opsForValue().get(environment.getProperty("ExamRoomIdKey"))+"-"+examinerDrawLot,
                                        exameeDrawLot,score);
        //创建考生分数hash
        redisTemplate.opsForHash().put((String)environment.getProperty("ExameeScoreKey")+"-"+redisTemplate.opsForValue().get(environment.getProperty("ExamRoomIdKey"))+"-"+exameeDrawLot,
                examinerDrawLot,score);
        return null;
    }

    @PostMapping("timeStart")
    public Result startTime(){
        //当前考场的当前考生,考生时间
        //获取当前的面试考生
        //校验考生的计时状态是否存在
        String examRoomId = examRoomService.getExamRoomId();
        //获取当前面试的考生号
        String exameeNumber="2";

        String examTimeKey = redisKeyUtils.spliceThreeRedisKey("ExamTimeRecordKey", examRoomId, exameeNumber);
        Boolean exist = redisTemplate.hasKey(examTimeKey);

        HashMap<Object, Object> map = new HashMap<>();
        if (exist) {
            String startTime  = (String)redisTemplate.opsForHash().get(examTimeKey, environment.getProperty("StartTimeField"));
            String endTime = (String) redisTemplate.opsForHash().get(examTimeKey, environment.getProperty("EndTimeField"));
            map.put(environment.getProperty("StartTimeField"), startTime);
            map.put(environment.getProperty("EndTimeField"), endTime);
        }else {
            String examTime = examRoomService.getExamTime();
            Date date = new Date();
            long startTime = date.getTime();

            int timeDistance = Integer.parseInt(examTime) * 60000;
            long endTime = startTime + timeDistance;

            map.put(environment.getProperty("StartTimeField"), String.valueOf(startTime));
            map.put(environment.getProperty("EndTimeField"), String.valueOf(endTime));
            redisTemplate.opsForHash().putAll(examTimeKey,map);
        }

        return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,map);


    }
}
